//
//  FriendlistView.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/5.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "FriendListView.h"
#import "FriendListCell.h"
#import "FriendListHeader.h"

@interface FriendListView()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *list_table_view;

@property (nonatomic, strong) UIView *tabView;

@property (nonatomic, strong) FriendListHeader *headerView;

@end

@implementation FriendListView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self makeTabViewUI];
        
        [self makeFriendListViewUI];
        
    }
    return self;
}


- (void)makeTabViewUI
{
    _tabView = [UIView new];
    [self addSubview:_tabView];
    [_tabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    
    UIButton *searchButton = [UIButton new];
    [searchButton addTarget:self action:@selector(clickSearchFriendButton:) forControlEvents:UIControlEventTouchUpInside];
    [searchButton setImage:[UIImage imageNamed:@"friend_search_button"] forState:UIControlStateNormal];
    [_tabView addSubview:searchButton];
    [searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(4);
        make.bottom.mas_equalTo(-4);
        make.right.mas_equalTo(-14);
        make.width.mas_equalTo(79);
    }];
}

- (void)makeFriendListViewUI
{
    UIView *mainView = [UIView new];
    mainView.layer.masksToBounds = YES;
    mainView.layer.borderWidth = 1.0;
    mainView.layer.borderColor = [RGB(240, 240, 240) CGColor];
    [self addSubview:mainView];
    [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(_tabView.mas_top).mas_offset(0);
    }];
    
    _headerView = [[FriendListHeader alloc]init];
    [mainView addSubview:_headerView];
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(30);
    }];
    
    _list_table_view = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _list_table_view.dataSource = self;
    _list_table_view.delegate = self;
    _list_table_view.separatorStyle = UITableViewCellSeparatorStyleNone;
    [mainView addSubview:_list_table_view];
    [_list_table_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headerView.mas_bottom).mas_offset(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    _list_table_view.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _list_table_view.tableFooterView = nil;
        if ([self.delegate respondsToSelector:@selector(headerWithRefreshingData)]) {
            [self.delegate headerWithRefreshingData];
        }
    }];
    

}

- (void)loadFriendListData
{
    [_list_table_view.mj_header beginRefreshing];
}

- (void)setListData:(NSArray *)listData
{
    _listData = listData;
    if (_listData.count == 0) {
        _list_table_view.tableFooterView = [[YJDataStateView sharedStateView] showStateWithTitle:@"暂无好友数据"];
    }
    [_list_table_view.mj_header endRefreshing];
    [_list_table_view reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FriendListCell *listCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FriendListCell class])];
    if (!listCell) {
        listCell = [[FriendListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([FriendListCell class])];
    }
    
    [listCell loadFriendListCellData:(FriendModel *)[_listData objectAtIndex:indexPath.row]];
    if (indexPath.row % 2 == 0) {
        listCell.backgroundColor = RGB(245, 242, 242);
    }
    
    __weak __typeof(self) weakSelf = self;
    listCell.moreBlock = ^(){
        CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
        CGRect rectInSuperview = [tableView convertRect:rectInTableView toView:[tableView superview]];
        [weakSelf didSelectMoreWithRect:rectInSuperview atIndex:indexPath.row];
    };
    
    return listCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    return 48;
}

- (void)didSelectMoreWithRect:(CGRect)rect atIndex:(NSInteger)index
{
    FriendModel *model = [_listData objectAtIndex:index];
    if ([self.delegate respondsToSelector:@selector(didSelectMore:friendModel:)]) {
        [self.delegate didSelectMore:rect friendModel:model];
    }
}

- (void)clickSearchFriendButton:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(clickSearchFriend:)]) {
        [self.delegate clickSearchFriend:sender];
    }
}

@end
